<?php
/**
 * WebSocket控制器基类
*/

namespace houze\controller;

class WebSocket {

    /**
     * 利用析构函数，自动回收归还连接池
    */
    public function __destruct() {
        $list = get_object_vars($this);
        foreach ($list as $name=>$value) {
            if (is_object($value)) {
                $obj = get_class($value);
                if ($obj == 'houze\\Db' || $obj == 'houze\\Redis') {
                    $this->$name->return();
                }
            }
        }
    }

    /** 
     * AES加密方法，对数据进行加密，返回加密后的数据
     */  
    private static function encrypt($data) {  
        $config = \houze\Config::get('server');
        return openssl_encrypt($data, $config['aes_type'], $config['aes_key'], 0, $config['aes_iv']);  
    }  
  
    /** 
     * AES解密方法，对数据进行解密，返回解密后的数据 
     */  
    private static function decrypt($data) {  
        $config = \houze\Config::get('server');
        return openssl_decrypt($data, $config['aes_type'], $config['aes_key'], 0, $config['aes_iv']);  
    } 

    /**
     * 获取参数
    */
    public final function param() {
        $data = $this->get_data();
        return $data['data'];
    }

    /**
     * 获取参数
    */
    public final function get_data() {
        $websocket_data = \houze\context\Container::get('websocket_data');
        $data = $websocket_data;
        $config = \houze\Config::get('server');
        // 启用加密方式
        if ($config['aes_key']) {
            $data = self::decrypt($data);
        }

        return json_decode($data, true);
    }

    /**
     * 更新参数
    */
    public final function set_data($data) {
        $config = \houze\Config::get('server');
        // 启用加密方式
        if ($config['aes_key']) {
            $data = self::encrypt(json_encode($data, JSON_UNESCAPED_UNICODE));
        }
        
        return \houze\context\Container::set('websocket_data', $data);
    }

    /**
     * 获取当前客户端fd标识
    */
    public final function get_current_fd() {
        $websocket_response = \houze\context\Container::get('websocket_response');
        return $websocket_response->id;
    }

    /**
     * 发送数据包
    */
    public final function fetch($action, $msg='success', $data=[], $fd=null, $websocket_server=null) {
        if (!$websocket_server) $websocket_response = \houze\context\Container::get('websocket_response');

        if (!$fd) {
            $websocket_response = \houze\context\Container::get('websocket_response');
            $fd = $websocket_response->id;
        }
        $config = \houze\Config::get('server');
        $array = [
            'action' => $action,
            'msg' => $msg,
            'data' => $data
        ];
        $content = json_encode($array, JSON_UNESCAPED_UNICODE);

        // 启用加密方式
        if ($config['aes_key']) {
            $content = self::encrypt($content);
        }

        try {
            $ret = $websocket_response->send($content);
            return $ret;
        } catch (\Exception $e) {
            // 生命周期回调
            return \houze\Lifecycle::websocket_push_error($websocket_response, $content, $fd);
        }
    } 

}